package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    @Override
    @Transactional
    @CacheEvict(value = "shoppingCart",key = "#currentId")
    public void add(ShoppingCartDTO shoppingCartDTO,Long currentId) {
        //添加购物车,添加的可能是菜品或套餐
        //根据购物车中数据的dishId和setmealId,dishFlavor 判断shoppingCart表中是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = getQueryWrapper(shoppingCartDTO);
        List<ShoppingCart> list = shoppingCartMapper.selectList(queryWrapper);

        Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
        Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());

        if (list != null && !list.isEmpty()){
            //如果存在，数量加1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateById(cart);
        }else {
            //不存在，则添加 //1.判断添加的是菜品还是套餐 菜品 要去菜品表，套餐要去套餐表 获取名字,图片，价格
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            if (shoppingCart.getDishId() != null){
                //添加的是菜品 查询菜品表
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else if (shoppingCart.getSetmealId() != null){
                //添加的是套餐
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);

        }
    }

    @Override
    public List<ShoppingCart> list() {
        //通过用户id 查询购物车 返回列表
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(queryWrapper);
    }

    @Override
    @CacheEvict(value = "shoppingCart",key = "#currentId")
    public void clean(Long currentId) {
        //根据用户id 清空购物车
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartMapper.delete(queryWrapper);
    }

    @Override
    @Transactional
    @CacheEvict(value = "shoppingCart",key = "#currentId")
    public void sub(ShoppingCartDTO shoppingCartDTO,Long currentId) {
        //根据用户id和菜品id,口味或套餐id 来锁定要删除的购物车数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = getQueryWrapper(shoppingCartDTO);
        ShoppingCart cart = shoppingCartMapper.selectOne(queryWrapper);
        if (cart != null){
            if (cart.getNumber() == 1){
                //数量减到0 则删除
                shoppingCartMapper.delete(queryWrapper);
            }else {
                //数量减1
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.updateById(cart);
            }
        }
    }
    /**
     * 动态查询条件构建
     * @param shoppingCartDTO
     * @return 根据用户id 动态查询购物车商品
     */
    private LambdaQueryWrapper<ShoppingCart> getQueryWrapper(ShoppingCartDTO shoppingCartDTO){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCartDTO.getDishId() != null, ShoppingCart::getDishId, shoppingCartDTO.getDishId());
        queryWrapper.eq(shoppingCartDTO.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCartDTO.getSetmealId());
        queryWrapper.eq(shoppingCartDTO.getDishFlavor() != null, ShoppingCart::getDishFlavor, shoppingCartDTO.getDishFlavor());
        return queryWrapper;
    }
}
